import { StoreOptions } from "vuex";
import axios from "axios";

export default {
  namespaced: true,
  state: () => ({
    currentUser: {
      username: "未登录",
    },
  }),
  actions: {
    async getLoginUser({ commit }) {
      // 从远程请求获取登录信息
      const res = await axios({
        method: "get",
        url: "http://localhost:8081/user/current",
      });
      if (res.data.code === 0) {
        commit("updateUser", res.data.data);
      }
    },
  },
  mutations: {
    updateUser(state, payload) {
      state.currentUser.username = payload.username;
    },
  },
} as StoreOptions<any>;
